package pkg_group5;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService()
public class MailingList {

    MailingListDatabase mld = new MailingListDatabase();

    // add a users details to the mailing list
    @WebMethod(operationName = "subscribe")
    public String subscribe(@WebParam(name = "firstname") String firstname, @WebParam(name = "surname") String surname, @WebParam(name = "emailaddress") String emailAddress, @WebParam(name = "listID") int listID) {
        // check email is new
        if (mld.checkEmailIsNew(emailAddress) == true) {
            // validate email
            Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
            //Match the given string with the pattern
            Matcher m = p.matcher(emailAddress);
            //check whether match is found
            boolean matchFound = m.matches();
            if (matchFound) {
                mld.subscribe(firstname, surname, emailAddress, listID);
                return "user added to mailing list";
            } else {
                return "invalid email address";
            }
        } else {
            return "Email already in use";
        }
    }

    // remove a user from the mailing list
    @WebMethod(operationName = "unsubscribe")
    public String unsubscribe(@WebParam(name = "emailAddress") String emailAddress) {
        // validate email
        Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
        //Match the given string with the pattern
        Matcher m = p.matcher(emailAddress);
        //check whether match is found
        boolean matchFound = m.matches();
        if (matchFound) {
            // check email is new
            if (mld.checkEmailIsNew(emailAddress) == false) {
                mld.unsubscribe(emailAddress);
                return "user removed from mailing list";
            } else {
                return "email address not found in database";
            }
        } else {
            return "invalid email address";
        }
    }

    // return all user details from the selected mailing list
    @WebMethod(operationName = "getListOfSubscribers")
    public ArrayList<MailingListUser> getListOfSubscribers(@WebParam(name = "listID") int listID) {
        if (mld.checkListIDExists(listID) == true) {
            ArrayList<MailingListUser> subscriberArrayList = mld.getListOfSubscribers(listID);
            return subscriberArrayList;
        } else {
            MailingListUser user = new MailingListUser();
            user.setFirstname("No users found with specified list id");
            user.setSurname("No users found with specified list id");
            user.setEmail("No users found with specified list id");
            ArrayList noListFound = new ArrayList();
            noListFound.add(user);
            return noListFound;
        }
    }
}
